package com.xinliu.fastchat.controller;


import com.xinliu.fastchat.domain.core.R;
import com.xinliu.fastchat.domain.dto.UserLoginDto;
import com.xinliu.fastchat.domain.dto.UserRegisterDto;
import com.xinliu.fastchat.domain.po.User;
import com.xinliu.fastchat.domain.vo.UserVo;
import com.xinliu.fastchat.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 心流
 * @since 2025-10-01
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final IUserService iFcUserService;

    //注册
    @PostMapping("/register")
    public R register(@RequestBody UserRegisterDto userRegisterDto) {
        if (userRegisterDto.getUsername() == null || userRegisterDto.getUsername().equals("") || userRegisterDto.getPassword() == null || userRegisterDto.getPassword().equals("")) {
            return R.error();
        }
        return iFcUserService.register(userRegisterDto);
    }

    //登录
    @PostMapping("/login")
    public R login(@RequestBody UserLoginDto userLoginDto) {
        if (userLoginDto.getUsername() == null || userLoginDto.getPassword() == null) {
            return R.error();
        }
        return iFcUserService.login(userLoginDto);
    }

    /**
     * 搜索用户
     *
     * @param userIdentify 用户表示符,包括 用户账号,用户名称
     * @return
     */
    @GetMapping("/search")
    public R search(String userIdentify) {
        List<User> list = iFcUserService.search(userIdentify);
        return R.ok(list);
    }

    //获得通过id获得用户
    @GetMapping("/user/{userId}")
    public R getUserById(@PathVariable Integer userId) {
        User user = iFcUserService.getById(userId);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return R.ok(userVo);
    }

}
